﻿/**
 * listUrl: 'marketActivePopupList',
 * searchForm: {},
    listParames: {
        type: 0
    },
    listApiFn(res){
        let apidata = res.list;
        apidata.forEach((el,ix) => {
            el._checked = (this.selectList.some(item => item.couponId === el.couponId));
        })
        return apidata;
    },
    //忽略
    recoveryFn(){
        this.selectData = {};
        this.selectList = [];
    },
 * 
 */
import { typeOf } from 'util';

export default {
    data() {
        return {
            tableLoading: false,
            apidata: [],
            upApiData: [],
            total: 0,
            pageSize: 20,
            pageIndex: 1,
        }
    },
    methods: {
        async GetList(callback) {
            this.tableLoading = true
            let searchForm = this.$cloneObj(this.searchForm);
            let parames = this.listParames || {};
            let requedata = Object.assign(searchForm,parames,{
                pageSize: this.pageSize,
                pageNum: this.pageIndex
            });
            let res = await this.$post(this.listUrl,requedata);
            let apidata = [];
            let total = 0;
            
            typeOf(this.beforeApiFn)==="function" && this.beforeApiFn.call(this,res);
            if(res){
                typeOf(this.resApiFn)==="function" && (res = this.resApiFn(res));
                if(res.list && typeOf(res.list)==="array"){
                    apidata = res.list;
                    total = res.total || 0;
                }else if(typeOf(res)==="array" && res.length){
                    apidata = res;
                };
                typeOf(this.listApiFn)==="function" && (apidata = this.listApiFn(res,apidata));
            };
			
			//页面更新，改变新数据，不影响原数据
            if(this.isUpdate){
                this.upApiData = apidata;
            }else{
                if(apidata && apidata.length) this._scrollTop();
                this.apidata = apidata;
            };
            
            this.total = total;
            this.tableLoading = false;
            return callback && callback();
        },
        //重新获取后滚动到顶部
        _scrollTop(){
            setTimeout(() => {
                document.getElementsByClassName('ivu-table-body')[0].scroll(0, 0);
            }, 100);
        },
        updateApi(){
            this.apidata = this.upApiData.slice(0);
            if(this.apidata.length) this._scrollTop();
        },
        search_click(){
            this.recoveryData(this.GetList);
            return false;
        },
        recoveryData(callback) {
            this.total = 0;
            // that.apidata = []
            this.pageIndex = 1;
            typeOf(this.recoveryFn)==="function" && this.recoveryFn();
            return callback && callback()
        },
        pageSizeChange(size){
            this.pageSize = size;
            return this.search_click();
        },
        pageChange(value){
            this.pageIndex = value;
            return this.GetList();
        },
    },
    mounted() {
        
    }
}
